package com.loe.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.annotation.Keep
import com.loe.ui.*
import com.loe.ui.api.IUiView
import com.loe.ui.api.ImageViewApi
import com.loe.ui.page.UiPage
import com.quickjs.JSObject

@Keep
@SuppressLint("AppCompatCustomView")
open class UiImageView : ImageView, IUiView
{
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        initXML(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        initXML(attrs)
    }

    private fun initXML(attrs: AttributeSet)
    {
//        val array = context.obtainStyledAttributes(attrs, R.styleable.UiImageView)
//
//        rate = array.getFloat(R.styleable.UiImageView_ui_rate, rate.toFloat()).toDouble()
//        auto = array.getBoolean(R.styleable.UiImageView_ui_auto, auto)
//        array.getColor(R.styleable.UiImageView_ui_colorFilter, -2).let {
//            if (it != -2) setColorFilter(it)
//        }
//
//        array.getString(R.styleable.UiImageView_ui_bg)?.let { bg = it }
//        array.getString(R.styleable.UiImageView_ui_src)?.let { src = it }
//
//        isAlpha = isClickable
//
//        array.recycle()
    }

    override val jsObject by lazy { ImageViewApi(this).jsObject }

    override fun setUI(ui: JSObject, uiPage: UiPage?): View
    {
        // click
        ui.gotHasBoolean("isClick") { setClick(it) }

        // rate
        ui.gotHasDouble("rate") { rate = it }
        // auto
        ui.gotHasBoolean("auto") { auto = it }
        // colorFilter
        ui.gotHasString("colorFilter") { setColorFilter(it.color) }
        // scaleType
        ui.gotHasString("scaleType") { scaleType = ScaleType.valueOf(it.toUpperCase()) }

        // src
        ui.gotHas("src")
        {
            if (it is JSObject)
            {
                setImageDrawable(it.shape)
            } else
            {
                this.src = it.toString()
            }
        }

        return this
    }

    override var onSelect: (() -> Unit)? = null

    /******************************************** 原生 ******************************************/

    /** 固定比例 */
    var rate = -1.0

    /** 是否自动高度 */
    var auto = false

    var urlString = ""

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
    {
        if (drawable == null)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            return
        }
        // 计算出ImageView的宽度
        val viewWidth = MeasureSpec.getSize(widthMeasureSpec)
        if (auto)
        {
            // 根据图片长宽比例算出ImageView的高度
            val viewHeight = viewWidth * drawable.intrinsicHeight / drawable.intrinsicWidth
            // 将计算出的宽度和高度设置为图片显示的大小
            setMeasuredDimension(viewWidth, viewHeight)
        } else if (rate > 0)
        {
            // 将计算出的宽度和高度设置为图片显示的大小
            setMeasuredDimension(viewWidth, (viewWidth * rate).toInt())
        } else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        }
    }

    /********************************************** 点击 ****************************************/

    fun setClick(isClick: Boolean)
    {
        isAlpha = isClick
        isClickable = isClick
    }

    private var isAlpha = false

    override fun dispatchSetPressed(pressed: Boolean)
    {
        if (isAlpha) alpha = if (pressed || !isEnabled) 0.75f else 1f
        super.dispatchSetPressed(pressed)
    }

    override fun setEnabled(enabled: Boolean)
    {
        if (isAlpha) alpha = if (enabled) 1f else 0.75f
        super.setEnabled(enabled)
    }
}